<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'BuyCar_Two.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link type="text/css" rel="stylesheet" href="statics/css/style.css" />
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

<script type="text/javascript"
	src="/EasyBuy/statics/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="/EasyBuy/statics/js/menu.js"></script>

<script type="text/javascript" src="/EasyBuy/statics/js/n_nav.js"></script>

<script type="text/javascript" src="/EasyBuy/statics/js/select.js"></script>

<script type="text/javascript" src="/EasyBuy/statics/js/num.js">
	var jq = jQuery.noConflict();
</script>

<script type="text/javascript" src="/EasyBuy/statics/js/shade.js"></script>

</head>

<body>
	<!-- 头部 -->
	<c:if test="${list1==null }">
		<%
			request.getRequestDispatcher(
						"../../IndexServlet?opr=type&hre=BuyCar_Two").forward(
						request, response);
		%>
	</c:if>
	<%@ include file="../../common/header.jsp"%>
	<%@ include file="../../common/preHeader.jsp"%>
	
	<!--Begin Header Begin-->
	<input type="hidden" name="wz" id="swz" value="<%= request.getParameter("wz")%>"/>
	<!--End Menu End-->
	<div class="i_bg">
		<div class="content mar_20">
			<img src="/EasyBuy/statics/images/img2.jpg"/>
		</div>

		<!--Begin 第二步：确认订单信息 Begin -->
		<div class="content mar_20">
			<div class="two_bg">
				<div class="two_t">
					<span class="fr"> </span>商品列表
				</div>
				<table border="0" class="car_tab" style="width:1110px;"
					cellspacing="0" cellpadding="0" id="qwe">
					<tr>
						<td class="car_th" width="550">商品名称</td>
						<td class="car_th" width="140">单价</td>
						<td class="car_th" width="150">购买数量</td>
						<td class="car_th" width="130">小计</td>
						<td class="car_th" width="140">返还积分</td>
					</tr>
					
					
					
					<tr>
						<td colspan="5" align="right" id="spzj"
							style="font-family:'Microsoft YaHei';"></td>
					</tr>
				</table>

				<div class="two_t">
					<span class="fr"><a href="#">修改</a> </span>收货人信息
				</div>
				<table border="0" class="peo_tab" style="width:1110px;"
					cellspacing="0" cellpadding="0" id="address">
					
					
				</table>


				<div class="two_t">配送方式</div>
				<table border="0" class="car_tab" style="width:1110px;"
					cellspacing="0" cellpadding="0">
					<tr>
						<td class="car_th" width="80"></td>
						<td class="car_th" width="200">名称</td>
						<td class="car_th" width="370">订购描述</td>
						<td class="car_th" width="150">费用</td>
						<td class="car_th" width="135">免费额度</td>
						<td class="car_th" width="175">保价费用</td>
					</tr>
					<tr>
						<td align="center"><input type="checkbox" name="ch"
							checked="checked" value="1"/></td>
						<td align="center" style="font-size:14px;"><b>申通快递</b></td>
						<td>江、浙、沪地区首重为15元/KG，其他地区18元/KG，续重均为5-6元/KG， 云南地区为8元</td>
						<td align="center">￥15.00</td>
						<td align="center">￥0.00</td>
						<td align="center">支持保价(按货品价格的3%来收取报价费用)</td>
					</tr>
					<tr>
						<td align="center"><input type="checkbox" name="ch" /></td>
						<td align="center" style="font-size:14px;"><b>城际快递</b></td>
						<td>运费固定</td>
						<td align="center">￥15.00</td>
						<td align="center">￥0.00</td>
						<td align="center">不支持保价</td>
					</tr>
					<tr>
						<td align="center"><input type="checkbox" name="ch" /></td>
						<td align="center" style="font-size:14px;"><b>邮局平邮</b></td>
						<td>运费固定</td>
						<td align="center">￥15.00</td>
						<td align="center">￥0.00</td>
						<td align="center">不支持保价</td>
					</tr>
					<tr>
						<td colspan="6"><span class="fr"><label class="r_rad"><input
									type="checkbox" name="baojia"/> </label><label class="r_txt">配送时需要保价</label>
						</span>
						</td>
					</tr>
				</table>

				<div class="two_t">支付方式</div>
				<ul class="pay">
					<li class="checked">余额支付
						<div class="ch_img"></div></li>
					<li>银行亏款/转账
						<div class="ch_img"></div></li>
					<li>货到付款
						<div class="ch_img"></div></li>
					<li>支付宝
						<div class="ch_img"></div></li>
				</ul>

				<div class="two_t">商品包装</div>
				<table border="0" class="car_tab" style="width:1110px;"
					cellspacing="0" cellpadding="0">
					<tr>
						<td class="car_th" width="80"></td>
						<td class="car_th" width="490">名称</td>
						<td class="car_th" width="180">费用</td>
						<td class="car_th" width="180">免费额度</td>
						<td class="car_th" width="180">图片</td>
					</tr>
					<tr>
						<td align="center"><input type="checkbox" name="pack"
							checked="checked" /></td>
						<td><b style="font-size:14px;">不要包装</b></td>
						<td align="center">￥0.00</td>
						<td align="center">￥0.00</td>
						<td align="center"></td>
					</tr>
					<tr>
						<td align="center"><input type="checkbox" name="pack" /></td>
						<td><b style="font-size:14px;">精品包装</b></td>
						<td align="center">￥15.00</td>
						<td align="center">￥0.00</td>
						<td align="center"><a href="#" style="color:#ff4e00;">查看</a>
						</td>
					</tr>
				</table>

				<div class="two_t">祝福贺卡</div>
				<table border="0" class="car_tab" style="width:1110px;"
					cellspacing="0" cellpadding="0">
					<tr>
						<td class="car_th" width="80"></td>
						<td class="car_th" width="490">名称</td>
						<td class="car_th" width="180">费用</td>
						<td class="car_th" width="180">免费额度</td>
						<td class="car_th" width="180">图片</td>
					</tr>
					<tr>
						<td align="center"><input type="checkbox" name="wish"
							checked="checked" /></td>
						<td><b style="font-size:14px;">不要贺卡</b></td>
						<td align="center">￥0.00</td>
						<td align="center">￥0.00</td>
						<td align="center"></td>
					</tr>
					<tr>
						<td align="center" style="border-bottom:0; padding-bottom:0;"><input
							type="checkbox" name="wish" /></td>
						<td style="border-bottom:0; padding-bottom:0;"><b
							style="font-size:14px;">祝福贺卡</b></td>
						<td align="center" style="border-bottom:0; padding-bottom:0;">￥15.00</td>
						<td align="center" style="border-bottom:0; padding-bottom:0;">￥0.00</td>
						<td align="center" style="border-bottom:0; padding-bottom:0;"><a
							href="#" style="color:#ff4e00;">查看</a></td>
					</tr>
					<tr valign="top">
						<td align="center"></td>
						<td colspan="4"><span class="fl"><b
								style="font-size:14px;">祝福语：</b> </span> <span class="fl"><textarea
									class="add_txt" style="width:860px; height:50px;"></textarea> </span>
						</td>
					</tr>
				</table>

				<div class="two_t">其他信息</div>
				<table border="0" class="car_tab" style="width:1110px;"
					cellspacing="0" cellpadding="0">
					<tr>
						<td width="145" align="right" style="padding-right:0;"><b
							style="font-size:14px;">使用红包：</b></td>
						<td><span class="fl"
							style="margin-left:50px; margin-right:10px;">选择已有红包</span> 
							
							<select id="red" style="position: absolute; top=1px; width: 100px;">
								<option value="0">--请选择--</option>
								<!-- 红包     value = 红包金额_红包编号 -->
								
							</select> 
							
							<span class="fl" style="margin-left:50px; margin-right:10px;">
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;或者输入红包序列号</span>
							<span class="fl"><input type="text" value="" class="c_ipt"
								style="width:220px;"/></span> <span class="fr"
								style="margin-left:10px;"><input type="submit"
									value="验证红包" class="btn_tj" id="btn_tj"/> </span>
						</td>
					</tr>
					<tr valign="top">
						<td align="right" style="padding-right:0;"><b
							style="font-size:14px;">订单附言：</b></td>
						<td style="padding-left:0;"><textarea class="add_txt"
								style="width:860px; height:50px;"></textarea></td>
					</tr>
					<tr>
						<td align="right" style="padding-right:0;"><b
							style="font-size:14px;">缺货处理：</b></td>
						<td><label class="r_rad"><input type="checkbox"
								name="none" checked="checked" /> </label><label class="r_txt"
							style="margin-right:50px;">等待所有商品备齐后再发</label> <label
							class="r_rad"><input type="checkbox" name="none" /> </label><label
							class="r_txt" style="margin-right:50px;">取下订单</label> <label
							class="r_rad"><input type="checkbox" name="none" /> </label><label
							class="r_txt" style="margin-right:50px;">与店主协商</label>
						</td>
					</tr>
				</table>

				<table border="0" style="width:900px; margin-top:20px;"
					cellspacing="0" cellpadding="0" id="settlement">
					
					<tr>
						<td align="right">该订单完成后，您将获得 <font color="#ff4e00" id="jf"></font>
							积分 ，以及价值 <font color="#ff4e00" id="red_"></font> 的红包 <br /> 商品总价: <font
							color="#ff4e00" id="spjg"></font> + 配送费用: <font color="#ff4e00" id="express"></font>
						</td>
					</tr>
					<tr height="70">
						<td align="right"><b style="font-size:14px;">应付款金额：<span
								style="font-size:22px; color:#ff4e00;" id="zong"></span> </b>
						</td>
					</tr>
					
					<tr height="70">
						<td align="right"><a id="js"><img
								src="/EasyBuy/statics/images/btn_sure.gif" /> </a></td>
					</tr>
				</table>
			</div>
		</div>
	</div>
<%@ include file="../../common/footer.jsp"%>
		
		
<script type="text/javascript" src="/EasyBuy/statics/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		var addressid = 0;	//地址id
		var total = 0;//总价格

		swz();
		
		function swz(){
			$(".leftNav").hide();		//全部分类3级菜单隐藏
			$.getJSON("/EasyBuy/BuyCarServlet","opr=loadGoods&userId=${applicationScope.id}&wz="+$("#swz").val(),function(data){		//加载商品
				for(var i = 0;i<data.list.length;i++){
					var s = data.list[i];
					var w = data.list1[i];
					var xj = s.price*w.productnum;	//小计
					var jf = (parseFloat(s.price)/10)*w.productnum;	//积分
					if(((i+1)%2) == 0){
						$("#qwe tr:eq(0)").after("<tr class='car_tr'><td><div class='c_s_img'><img src='/EasyBuy/statics/images/"+s.fileName+"' width='73' height='73'/></div> "+s.name+"</td><td align='center'>"+s.price+"</td><td align='center'>"+w.productnum+"</td><td align='center' class='xj' name='xj' style='color:#ff4e00;''>￥"+xj+"</td><td align='center'>"+jf.toFixed(2)+"R</td></tr>");
					}else{
						$("#qwe tr:eq(0)").after("<tr><td><div class='c_s_img'><img src='/EasyBuy/statics/images/"+s.fileName+"' width='73' height='73'/></div> "+s.name+"</td><td align='center'>"+s.price+"</td><td align='center'>"+w.productnum+"</td><td align='center' class='xj' name='xj' style='color:#ff4e00;''>￥"+xj+"</td><td align='center'>"+jf.toFixed(2)+"R</td></tr>");
					}
				}
				countGoods();
			});
			$.getJSON("/EasyBuy/BuyCarServlet","opr=loadAddress&userId=${applicationScope.id}",function(data){		//加载地址信息
				for(var i = 0;i<data.length;i++){
					var addres = data[i];
					addressid = addres.id;
					$("#address").append("<tr><td class='p_td' width='160'>收货人名称</td><td width='395'>"+addres.name+"</td><td class='p_td' width='160'>电子邮件</td><td width='395'>"+addres.email+"</td></tr><tr><td class='p_td'>详细信息</td><td>"+addres.address+"</td><td class='p_td'>邮政编码</td><td>044000</td></tr><tr><td class='p_td'>电话</td><td></td><td class='p_td'>手机</td><td>"+addres.telephone+"</td></tr>");
				}
			});
			$.getJSON("/EasyBuy/BuyCarServlet","opr=loadRed&userId=${applicationScope.id}",function(data){	//加载我的红包
				for(var i = 0;i<data.length;i++){
					var s = data[i];
					$("#red").append("<option value='"+s.money+"_"+s.serial+"'>"+s.money+"元</option>");
				}
			});
		}

		
		function addOrder(){		//添加订单
			var OrderId = 0;		//订单id
			var zong = $("#zong").text();		//总金额
			var commodityId = $("#swz").val();	//所有要购买商品的商品id;
			var express = "";
			if($("input[name=ch]:eq(0)").attr("checked")){
				express = "申通快递";
			}else if($("input[name=ch]:eq(1)").attr("checked")){
				express = "城际快递";
			}else if($("input[name=ch]:eq(2)").attr("checked")){
				express = "邮局平邮";
			}
			$.get("/EasyBuy/BuyCarServlet","opr=address&addressid="+addressid,function(data){		//查找收货地址
				$.get("/EasyBuy/BuyCarServlet","opr=addOrder&id=${applicationScope.id}&name=${applicationScope.name}&order="+data+"&money="+total,function(data1){	//添加订单 		返回订单id
					if(data1 != "sb"){	//当返回的值不等于失败时
						$.get("/EasyBuy/BuyCarServlet","opr=details&id=${applicationScope.id}&wz="+commodityId+"&OrderId="+data1,function(data2){	//添加订单详情
							if(data2 == "cg"){
								$.get("/EasyBuy/BuyCarServlet","opr=updat&id=${applicationScope.id}&total="+total,function(data3){		//修改用户表中 消费金额和获得积分
									if(data3 == "cg"){
										$.get("/EasyBuy/BuyCarServlet","opr=delebuycar&id=${applicationScope.id}&wz="+commodityId,function(data5){		//删除已购买的商品
											if(data5 == "cg"){
												$.get("/EasyBuy/BuyCarServlet","opr=record&uId=${applicationScope.id}&total="+total,function(data4){		//添加消费记录
													if(data4 == "cg"){
														$.get("/EasyBuy/BuyCarServlet","opr=oddNum&OrderId="+data1,function(data6){		
																if($(".pay li:eq(0)").hasClass("checked")){
																	location.href="/EasyBuy/pre/settlement/BuyCar_Three.jsp?mode=1&Orderid="+data6+"&money="+zong+"&express="+express;	//余额
																}else if($(".pay li:eq(1)").hasClass("checked")){
																	location.href="/EasyBuy/pre/settlement/BuyCar_Three.jsp?mode=2&Orderid="+data6+"&money="+zong+"&express="+express;	//银行卡
																}else if($(".pay li:eq(2)").hasClass("checked")){
																	location.href="/EasyBuy/pre/settlement/BuyCar_Three.jsp?mode=3&Orderid="+data6+"&money="+zong+"&express="+express;	//货到付款
																}else if($(".pay li:eq(3)").hasClass("checked")){
																	location.href="/EasyBuy/pre/settlement/BuyCar_Three.jsp?mode=4&Orderid="+data6+"&money="+zong+"&express="+express;	//支付宝
																}
															
														});
													}else if(data4 == "sb"){
														alert("提交订单失败,请联系平台管理员!");
													}
												});
											}else{
												alert("提交订单失败,请联系平台管理员!");
											}
										});
									}else if(data3 == "sb"){
										alert("提交订单失败,请联系平台管理员!");
									}
								});
							}else{
								alert("提交订单失败,请联系平台管理员!");
							}
						});
					}
				});
			});
		}
		
		$("#js").click(function(){		//点击确订单
			if($(".pay li:eq(0)").hasClass("checked")){
				$.get("/EasyBuy/BuyCarServlet","opr=judge&id=${applicationScope.id}",function(data){	//判断用户余额是否够
					if(parseFloat(data) >= parseFloat(total)){
						$.get("/EasyBuy/BuyCarServlet","opr=updatbalance&id=${applicationScope.id}&total="+total,function(data4){		//修改用户表中  余额
							if(data4 == "sb"){
								alert("提交订单失败,请联系平台管理员!");
								break;
							}
						});
						addOrder();		//添加订单
					}else{
						location.href="/EasyBuy/pre/settlement/BuyCar_Three.jsp?mode=0";	//余额不足
					}
				});	
			}else{
				addOrder();		//添加订单
				
				
			}
			
		});
		
		function countTotal(){	//计算总价格
			alert($("#price").text());
		}
		
		//输入红包序列号		徐炜杰
		$("#btn_tj").click(function(){
			var hao=$(".c_ipt").val();
			if(hao==""){
				alert("请输入序列号");
			}else{
				$.get("/EasyBuy/BuyCarServlet","opr=Yan&serial="+hao,function(datan){
					alert(datan);
					$(".c_ipt").val("");
					$.getJSON("/EasyBuy/BuyCarServlet","opr=loa&id=2&serial="+hao,function(dat){		
						for(var i = 0;i<dat.length;i++){
							var s = dat[i];
							$("#red").append("<option value='"+s.money+"_"+s.serial+"' selected='selected'>"+s.money+"元</option>");
						}
					});
				});
			}
		});
		
		function countGoods(){	//计算商品价格
			var prices=document.getElementsByName("xj");
			var price = 0;		//商品价格
			var express = 0;		//快递费用
			var red = parseInt($("#red").val().split("_")[0]);		//红包价值
			total = 0;		//总价格	快递费+商品费用-红包
			
			for(var i=0; i<prices.length;i++){		//计算商品价格
				var h = prices[i].innerHTML.substring(prices[i].innerHTML.lastIndexOf("￥")+1);	//分割￥符号
       			price= parseFloat(price)+parseFloat(h);
    		}
   			var jf = parseFloat(price/10);			//积分
   			$("#spzj").html("商品总价：<span id='price'>￥"+price+"</span>； 返还积分"+jf+"R");

   			for(var i = 0;i<$("input[name='pack']").length;i++){		//计算包装的价格
   				if($("input[name='pack']:eq("+i+")").is(':checked')){
   					if($("input[name='pack']:eq("+i+")").parent().next().children().text() == "精品包装"){
   						price = parseFloat(price)+parseFloat(15);	//加15元包装费
   					}
   				}
   			}
   			
   			for(var i = 0;i<$("input[name='wish']").length;i++){		//计算贺卡的价格
   				if($("input[name='wish']:eq("+i+")").is(':checked')){
   					if($("input[name='wish']:eq("+i+")").parent().next().children().text() == "祝福贺卡"){
   						price = parseFloat(price)+parseFloat(15);	//加15元贺卡费
   					}
   				}
   			}
   			
   			for(var i = 0;i<$("input[name='ch']").length;i++){		//计算快递的价格
   				if($("input[name='ch']:eq("+i+")").is(':checked')){
   					if($("input[name='ch']:eq("+i+")").parent().next().children().text() == "申通快递"){
   						if($("input[name='baojia']").attr("checked")){
   							express = parseFloat((price*0.03)+15);
   						}else{
   							express = parseFloat(express+15);	//加15元快递费
   						}
   					}else{
   						express = parseFloat(express+15);	//加15元快递费
   					}
   				}
   			}

   			total = parseFloat(price+express-red);		//计算总价格
   			$("#jf").html(jf);		//积分
   			$("#red_").html("￥"+red);	//红包
   			$("#spjg").html("￥"+price);	//商品价格
   			$("#express").html("￥"+express);	//配送费用
   			$("#zong").html("￥"+total);		//总价格
		}
		
		
		var preUrl = document.referrer;		//获得进入本页面的地址
		if(preUrl == ""){
			//alert(1);
		}
		
		$("#red").change(function(){		//红包下拉框改变事件
			countGoods();
		});
		
		$("input[name='ch']").click(function(){			//点击快递
			$("input[name='ch']").not(this).attr("checked", false);
			if($(this).parent().next().children().text() == "申通快递"){		//判断选中的是否是申通快递
				if($("input[name='baojia']")){
					$("input[name='baojia']").removeAttr("disabled");
				}
			}else{
				$("input[name='baojia']").attr("checked", false);		//让保质设置未选中状态
				$("input[name='baojia']").attr("disabled",true);		//让保质设置禁用状态
			}
			countGoods();
		});
		
		$("input[name='baojia']").click(function(){	//单机保价
			countGoods();
		});
		
		$(".pay li").click(function(){		//点击支付方式
			$(".pay li").not(this).attr("class","");
			$(this).attr("class","checked");
			
		});
		
		$("input[name='pack']").click(function(){			//点击包装
			$("input[name='pack']").not(this).attr("checked", false);
			countGoods();
		});
		
		$("input[name='wish']").click(function(){			//点击贺卡
			$("input[name='wish']").not(this).attr("checked", false);
			countGoods();
		});
		
		$("input[name='none']").click(function(){			//点击缺货处理
			$("input[name='none']").not(this).attr("checked", false);
			countGoods();
		});
		
	});
	
</script>
</body>
</html>
